$(function(){
    $(window).on("load",()=>{
        class Register{
            constructor(){
                //获取信息
                this.btn       = $("#register_btn")
                this.username  = $("#username")
                this.nickname  = $("#nickname")
                this.password  = $("#password")
                this.rpassword = $("#rpassword")
                console.log(this.nickname);
                //提示信息
                this.alert = $("#register-alert")
                this.bindEvent();
            }
            bindEvent(){    
               
                this.btn.click(()=>{
                    this.register();
                    console.log(this.nickname.val());
                })
            }
            register(){
                console.log(this.username.val());
             var _this = this;
                $.ajax({
                    url : "http://127.0.0.1:8888/users/register",
                    data : {
                        username : this.username.val(),
                        nickname : this.nickname.val(),
                        password : this.password.val(),
                        rpassword : this.rpassword.val()
                    },
                    type : "POST" ,
                    dataType : "json"
                }).done(function(res){
                    console.log(res);
                    switch(res.code){
                        case 0 :
                            _this.error(res);
                            break;
                        case 1 :
                            _this.success(res);
                            break;
                    }
                    
                }); 
               
            }
            error(res){
                    this.alert.removeClass(["alert-success" , "alert-danger"]);
                    this.alert.addClass("alert-danger");
                    this.alert.html( "抱歉，注册失败 : " + res.message);
                    setTimeout(()=>{
                        this.alert.removeClass("alert-danger")
                    },2000)
            }
            success(res){
                this.alert.removeClass("alert-success","alert-danger");
                this.alert.addClass("alert-success");
                this.alert.html( "注册成功，稍后我们将为您跳转页面")
                setTimeout(()=>{
                    location.href = "./login.html";
                    sessionStorage.setItem("usermsg",JSON.stringify({
                        username : this.username.val(),
                        password : this.password.val()
                    }));
                },2000)
            }
        } 
         new Register
    })
   
})